/**************************************************************************(C)*
 *
 *  This file is part of the GreenHills project
 *  Copyright (C) 2009  Tieto Corporation
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  Not for commmercial use. Only with Tieto permission.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************************************************************************(C)*/

#include "../synchronouschannel.h"

#include "io/ioexception.h"
#include "io/connection.h"
#include "util/binstream.h"
#include "util/memstreambuf.h"
#include "event/event.h"
#include "event/actionevent.h"

#if defined Q_OS_WIN32
    #include <windows.h>
#elif defined Q_OS_WINCE
    #include <Winbase.h>
#endif


using namespace greenhills::io;
using namespace greenhills::event;
using namespace greenhills::util;

CSynchronousChannel::CSynchronousChannel(QObject *pParent,
        CConnection *pConnection, unsigned int pTimeout)
    :QObject(pParent), IDataListener(),
     mConnection(pConnection), mResponse(0), mTimeout(pTimeout)
{
    mConnection->addDataListener(this);
}

CActionEvent *CSynchronousChannel::sendMessage(const CActionEvent &pRequest)
{
    memstreambuf buffer;
    obinstream os(&buffer);
    CEvent::serialize(&pRequest, os);

    unsigned int length = buffer.length();
    const char *data = buffer.data();

    try {
#ifdef __LINUX__
        int clo = clock();
#elif __WIN32__
        int clo = GetTickCount();
#endif
        mResponse = 0;

        mConnection->sendMessage(data, length);

        while (mResponse==0) {
            mConnection->process();
#ifdef __LINUX__
            usleep(10);
            //! \todo sleep functio\n
            if (clock() - clo > mTimeout)
                return mResponse;
#elif __WIN32__
            Sleep(10);
            //! \todo sleep functio\n
            if (GetTickCount() - clo > mTimeout)
                return mResponse;
#endif
        }
    }
    catch(io::CIOException ioe) {
        return mResponse;
    }

    return mResponse;
}


void CSynchronousChannel::dataReceived(const char *pData,
            unsigned int pLength,
            CConnection *pConnection)
{
    Q_UNUSED(pConnection);

    memstreambuf buffer(pData, pLength);
    ibinstream is(&buffer);
    CEvent *response = CEvent::deserialize(is);
    Q_ASSERT(response != 0);

    mResponse = dynamic_cast<CActionEvent*>(response);

    if (mResponse == 0)
        delete response;
}
